package org.jeecg.modules.xmss.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.DySmsHelper;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.xmss.entity.XmssIdea;
import org.jeecg.modules.xmss.entity.XmssSupervise;
import org.jeecg.modules.xmss.entity.XmssTowns;
import org.jeecg.modules.xmss.mapper.XmssIdeaMapper;
import org.jeecg.modules.xmss.mapper.XmssSuperviseMapper;
import org.jeecg.modules.xmss.service.IXmssIdeaService;
import org.jeecg.modules.xmss.service.IXmssSuperviseService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class TimedTaskController implements Job {

    @Autowired
    private IXmssIdeaService xmssIdeaService;



    /**
     *  定时任务凌晨执行，查询三天前的已完成但是未好评的时间，进行默认打分
     *
     * @param
     * @return
     */


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        LambdaQueryWrapper<XmssIdea> query = new LambdaQueryWrapper<XmssIdea>();
        List<XmssIdea> getXmssIdea=new ArrayList<XmssIdea>();
        //查询已完成的事件
        query.eq(XmssIdea::getAppState,3);
        //查询大于三天的数据
        Calendar calendar1 = Calendar.getInstance();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        calendar1.add(Calendar.DATE, -3);
        String three_days_ago = sdf1.format(calendar1.getTime());
        query.lt(XmssIdea::getAnswerTime,three_days_ago);
        //分页查询
        Page<XmssIdea> page = new Page<XmssIdea>(1, 200);
        IPage<XmssIdea> pageList = xmssIdeaService.page(page, query);
        //对已完成且未打分的事件进行默认五星打分
        pageList.getRecords().stream().forEach(e -> {
            XmssIdea getXmssIdeaOne=new XmssIdea();
            getXmssIdeaOne.setId(e.getId());
            getXmssIdeaOne.setSupervise(5);
            getXmssIdeaOne.setStarDb(5);
            /* getXmssIdea.add(getXmssIdeaOne);*/
            xmssIdeaService.updateMain(getXmssIdeaOne);
        });

    }




   /* *//**
     *  定时任务上午九点执行，查询大于七天的未读事件发短信给督办
     *
     * @param
     * @return
     *//*

    public void noteInform(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JSONObject obj = new JSONObject();
        LambdaQueryWrapper<XmssSupervise> query = new LambdaQueryWrapper<XmssSupervise>();

        Calendar calendar1 = Calendar.getInstance();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        calendar1.add(Calendar.DATE, -7);
        String three_days_ago = sdf1.format(calendar1.getTime());
        query.lt(XmssSupervise::getCreateTime,three_days_ago);
        //分页查询
        Page<XmssSupervise> page = new Page<XmssSupervise>(1, 200);
        IPage<XmssSupervise> pageList = xmssSuperviseService.page(page, query);
        pageList.getRecords().stream().forEach(e -> {
            SysUser getUser = xmssSuperviseMapper.selectUserId(e.getUserId());
            XmssTowns xmssTowns = xmssIdeaMapper.selectAffair(e.getIdeaId());
            try {
                DySmsHelper.sendSmsZll(getUser.getUsername(), obj, xmssTowns.getTodoTemplateCode(), xmssTowns.getSign());
            } catch (ClientException ex) {
                ex.printStackTrace();
            }




        });
    }*/
}
